package by.msokoloff.javaweb2015.webapp.page.country;

import javax.inject.Inject;

import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.IModel;

import by.msokoloff.javaweb2015.datamodel.Country;
import by.msokoloff.javaweb2015.services.CountryService;
import by.msokoloff.javaweb2015.webapp.page.EntityListPage;

public class CountryForm extends Form<Country> {
	
	@Inject
	private CountryService countryService;
	
	private Country country;

	public CountryForm(String id, IModel<Country> model) {
		super(id, model);
		add(new Label("id"));
		TextField<String> nameTextField = new TextField<String>("name");
		nameTextField.add(new PropertyValidator<String>());
		add(nameTextField);
		TextField<String> isoCodeTextField = new TextField<String>("isoCode");
		isoCodeTextField.add(new PropertyValidator<String>());
		add(isoCodeTextField);
		add(new FeedbackPanel("feedbackpanel"));
		country = model.getObject();
	}
	
	@Override
	public final void onSubmit() {
		countryService.createOrUpdate(country);
		setResponsePage(new EntityListPage(Country.class));
	}
	
	@Override
	public final void onError() {
		System.out.println("Error!");
	}
}
